package org.game.util;

import lombok.SneakyThrows;

import java.lang.reflect.Field;
import java.util.Map;

public class MapUtil {
    public static void cloneMap(Map<String, Object> resourceMap, Map<String, Object> targetMap) {
        targetMap.forEach((k, v) -> {
            resourceMap.put(k, v);
        });
    }

    @SneakyThrows
    public static <T> T mapToEntity(Map<String, Object> map, Class<T> clazz) {
        T t = clazz.getDeclaredConstructor().newInstance();
        map.forEach((k, v) -> {
            try {
                Field field = t.getClass().getDeclaredField(k);
                field.setAccessible(true);
                field.set(t, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        return t;
    }

    /**
     * 判断是否越界
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     */
    public static boolean isOutOfBounds(int x, int y, int width, int height) {
        if (x == -1 || x == width || y == -1 || y == height) {
            return true;
        }

        return false;
    }
}
